use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use sqlx::{FromRow, Pool, Postgres};

#[derive(Debug, Clone, Serialize, Deserialize, FromRow, Default)]
pub struct SysChannel {
    pub id: Option<i32>,
    pub name: Option<String>,
    pub avatar: Option<String>,
    pub r#type: Option<i16>,
    pub parent_id: Option<i32>,
    pub create_time: Option<NaiveDateTime>,
    pub update_time: Option<NaiveDateTime>,
}

impl SysChannel {
    pub async fn _select_all(pool: &Pool<Postgres>) -> Vec<Self> {
        let sql = "SELECT * FROM sys_channel";
        sqlx::query_as::<_, Self>(sql).fetch_all(pool).await.unwrap()
    }

    pub async fn _select_name(pool: &Pool<Postgres>, name: &str) -> Self {
        let sql = "SELECT * FROM sys_channel where id = ?";
        sqlx::query_as::<_, Self>(sql).bind(name).fetch_one(pool).await.unwrap()
    }
}